//先包含jq

//var socketServer = "192.168.3.222:8080";
function socketInit(socketServer){
    var wsURL = "ws://"+socketServer+"/websocket";
    var websocket = null;
    if('WebSocket' in window){
        websocket = new WebSocket(wsURL);
    }else {
        alert("Not support websocket")
    }

    websocket.onerror = function(){
        alert("Socket link failed");
    };

    window.onbeforeunload = function(){
        websocket.close();
    }

    websocket.onmessage = function(event){
        //checkAlert();
        var jsonObj = JSON.parse(event.data).data;
        console.log(jsonObj);
        layer.closeAll();
        alertIdArr = [];
        for(var i in jsonObj){
            var obj = {
                id:jsonObj[i].alarm_origin_id,
                location:jsonObj[i].geo,
                name:jsonObj[i].name,
                dev_status:jsonObj[i].dev_status,
                picture:jsonObj[i].geo_level_3_id,
                pos_x:jsonObj[i].pos_x,
                pos_y:jsonObj[i].pos_y,
                sensor_name:jsonObj[i].sensor_name,
            }
            alertIdArr.push(obj.id);
            layer.open({
                type:1,
                area: ['800px', '600px'],
                title: '在'+obj.location+'发生'+obj.dev_status+obj.id,
                skin:'layui-layer-lan',
                content: '<div class="row" style="width: 99%;height: 99%">'
                +'<audio src="assets/报警铃声.mp3" autoplay="autoplay" loop="loop" displayed></audio>'
                +'  <section class="invoice">'
                +'       <div id="iPicture_layer'+obj.id+'" data-interaction="hover" style="width: 100%;height: 100%">'
                +'           <div class="ip_slide" id="map_div_layer'+obj.id+'" style="width: 100%;height: 100%">'
                +'           </div>'
                +'       </div>'
                +'   </section>'
                +'</div>',
                btn:['确认','误报','忽略'],
                btn1:function (index, layero) {
                    console.log("误报");
                    confirmAlert(alertIdArr.pop());
                    layer.close(index);
                },
                btn2: function (index, layero) {
                    console.log("误报");
                    denialAlert(alertIdArr.pop());
                    layer.close(index);
                },
                btn3:function (index, layero) {
                    console.log("忽略");
                    ignoreAlert(alertIdArr.pop());
                    layer.close(index);
                }
                ,zIndex: layer.zIndex
                ,success: function(layero){
                    layer.setTop(layero);
                    var imgs = "";
                    imgs = '<img id="show_map_layer" style="width: 100%;height: 100%" class="ip_tooltipImg"  src="'+server+'/xk/xkGeoLocation/mapDownload?id='+obj.picture+'" />';
                    $('#map_div_layer'+obj.id).append(imgs);
                    var x = (parseFloat(obj.pos_x) * 100-0.7);
                    x = x>0?x+'%':0;
                    var y = (parseFloat(obj.pos_y) * 100-0.9);
                    y = y>0?y+'%':0;
                    var divs ="";
                    divs ='<div class="ip_tooltip ip_img32" style="top: '+y+'; left: '+x+';" data-button="moreblue" data-tooltipbg="bgwhite" data-round="roundBgR" data-animationtype="btt-slide">' +
                        '</div>';
                    $('#map_div_layer'+obj.id).append(divs);
                    $( "#iPicture_layer"+obj.id ).iPicture_base();
                }
            })
        }
    };
}

//function run() {
//    interval = setInterval(checkAlert, '10000');
//}
//主动查询报警
function checkAlert(server) {
    console.log("server in function:"+server);
    $.ajax({
        url: server + '/xk/alarm/unHandler',
        success: function (data) {
            var jsonObj = JSON.parse(data).data;
            layer.closeAll();
            if(jsonObj.length){
                console.log(jsonObj);
                layer.closeAll();
                alertIdArr = [];
                for(var i in jsonObj){
                    var obj = {
                        id:jsonObj[i].alarm_origin_id,
                        location:jsonObj[i].geo,
                        name:jsonObj[i].name,
                        dev_status:jsonObj[i].dev_status,
                        picture:jsonObj[i].geo_level_3_id,
                        pos_x:jsonObj[i].pos_x,
                        pos_y:jsonObj[i].pos_y,
                        sensor_name:jsonObj[i].sensor_name,
                    }
                    alertIdArr.push(obj.id);
                    layer.open({
                        type:1,
                        area: ['800px', '600px'],
                        title: '在'+obj.location+'发生'+obj.dev_status+obj.id,
                        skin:'layui-layer-lan',
                        content: '<div class="row" style="width: 99%;height: 99%">'
                        +'<audio src="assets/报警铃声.mp3" autoplay="autoplay" loop="loop" displayed></audio>'
                        +'  <section class="invoice">'
                        +'       <div id="iPicture_layer'+obj.id+'" data-interaction="hover" style="width: 100%;height: 100%">'
                        +'           <div class="ip_slide" id="map_div_layer'+obj.id+'" style="width: 100%;height: 100%">'
                        +'           </div>'
                        +'       </div>'
                        +'   </section>'
                        +'</div>',
                        btn:['确认','误报','忽略'],
                        btn1:function (index, layero) {
                            console.log("误报");
                            confirmAlert(alertIdArr.pop());
                            layer.close(index);
                        },
                        btn2: function (index, layero) {
                            console.log("误报");
                            denialAlert(alertIdArr.pop());
                            layer.close(index);
                        },
                        btn3:function (index, layero) {
                            console.log("忽略");
                            ignoreAlert(alertIdArr.pop());
                            layer.close(index);
                        }
                        ,zIndex: layer.zIndex
                        ,success: function(layero){
                            layer.setTop(layero);
                            var imgs = "";
                            imgs = '<img id="show_map_layer" style="width: 100%;height: 100%" class="ip_tooltipImg"  src="'+server+'/xk/xkGeoLocation/mapDownload?id='+obj.picture+'" />';
                            $('#map_div_layer'+obj.id).append(imgs);
                            var x = (parseFloat(obj.pos_x) * 100-0.7);
                            x = x>0?x+'%':0;
                            var y = (parseFloat(obj.pos_y) * 100-0.9);
                            y = y>0?y+'%':0;
                            var divs ="";
                            divs ='<div class="ip_tooltip ip_img32" style="top: '+y+'; left: '+x+';" data-button="moreblue" data-tooltipbg="bgwhite" data-round="roundBgR" data-animationtype="btt-slide">' +
                                '</div>';
                            $('#map_div_layer'+obj.id).append(divs);
                            $( "#iPicture_layer"+obj.id ).iPicture_base();
                        }
                    })
                }
            }
        }
    });
}
//
function confirmAlert(id) {
    $.ajax({
        url:server + '/xk/alarm/confirm?alarm_id='+ id,
        //async:false,
        success:function (data) {
            console.log(id+'报警确认：'+data)
        }
    });
}
function ignoreAlert(id){
    console.log("报警忽略");
    $.ajax({
        url:server + '/xk/alarm/ignore?alarm_id='+ id,
        //async:false,
        success:function (data) {
            console.log(id+'报警忽略：'+data)
        }
    });
}
function denialAlert(id){
    console.log("误报");
    $.ajax({
        url:server + '/xk/alarm/denial?alarm_id='+ id,
        //async:false,
        success:function (data) {
            console.log(id+'误报：'+data)
        }
    });
}
